<?php
class MemberAction extends FrontAction {
    public function login() {
        if ($this->checkConsoleLogin()){
            $this->redirect('main/index');
            exit();
        }
        $this->assign('redirect_url',cookie('redirect_url') ? cookie('redirect_url') : U('memcp/index'));
        $this->display();
    }

    /**
     * 用户登陆操作
     */
    public function loginPost(){
        if($this->post) {
            $member = M('member');
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $verify = $_POST['verify'];
            if(!$this->validTR(true)) {
                alert('非法','',2);
                exit();
            }
            if(md5($verify) !== $_SESSION['verify']) {
                alert('验证码不正确','member/login');
                exit;
            }
            if (empty($_POST['username']) || empty($_POST['password'])){
                alert('没有万能帐号...','',2);
                exit();
            }

            if( $user = $member->where(array('username'=>$username,'rule'=>md5(sha1(C('auth_key')))))->find() ) {

                if($user['password'] != $password ){
                    alert('密码错误','',2);
                    exit();
                }
                $this->user = $user;
                $_SESSION['remeber'] == 'true';
                $expiry = $_POST['remember'] == 'true' ? 31536000 : 0;
                $this->setAuthCookie($expiry);
                $member->where(array('mid'=>$this->user['mid']))->data(array('lastlogin'=>time()))->save();
				cookie('redirect_url',null);
                alert('登录成功','main/index');
                exit();
            } else {
                alert('没权限','',2);
                exit();
            }
            alert('登录成功','main/index');
        }
    }

    /**
     * 会员登出
     */
    public function logout() {
        $this->disAuthCookie();
        $this->redirect('main/index');
    }

    /**
     * 验证码
     */
    public function verify() {
        import("ORG.Util.Image");
        Image::buildImageVerify($length=5,$mode=2,$type='png',$width=68,$height=27,$verifyName='verify');
    }

    public function _empty() {
        $this->redirect('member/login');
        exit;
    }

}